<style scoped>
.wDele {
  margin-right: 15px;
  padding: 0 15px;
  background-color: white;
  height: 87.5vh;
  position: relative;
}
.w-top {
  display: flex;
  padding-top: 15px;
  white-space: nowrap;
}
.t_content {
  margin: 0 8px 0 12px;
  padding-top: 3px;
  display: inline-block;
}
.t_content1 {
  padding-top: 3px;
  margin: 0 6px 0 0;
}
.wDele input:focus {
  outline: none;
  border: 1px solid #017aff;
}
.inp {
  border: 1px solid #dbdbdb;
  border-radius: 3px;
  height: 30px;
  padding: 0 15px;
}
.inp2 {
  width: 155px;
}
.inp1 {
  width: 400px;
}
.inp3 {
  width: 60px;
  margin-right: 10px;
}
.inp4 {
  width: 90px;
  margin-left: -1px;
}
.inp5 {
  width: 150px;
  margin: 0 0 0 16px;
}
.a_color {
  color: #017aff;
}
.deleteDocument_c {
  display: flex;
  width: 100%;
  height: 88%;
  margin-top: 20px;
  border-top: 2px solid #f4f4f4;
}
/*                   566464646*/
.content1 {
  width: 30%;
  border-right: 2px solid #f4f4f4;
  padding-right: 10px;
}
.content2 {
  width: 70%;
  /* height: 100%; */
  padding-left: 10px;
}
.content1_1 {
  display: flex;
  margin: 12px 0;
  overflow: hidden;
}
.c_button {
  width: 35px;
  height: 30px;
  margin: 0 0 0 -1px;
  /* padding:4px 6px 0 0; */
  background: #dbdbdb;
  font-size: 13px;
  border: 0px solid #dbdbdb;
  outline: none;
}
.hand {
  cursor: pointer;
}
.c2 {
  display: flex;
  height: 40px;
  margin-top: 10px;
  flex-wrap: nowrap;
  justify-content: space-between;
}
.c2_2 {
  display: flex;
  /* margin-left: 115px; */
  height: 40px;
}
.c2_1 {
  display: flex;
  height: 40px;
}
.c2_2 p {
  margin: 4px 16px 0 0;
}
.check {
  width: 90px;
  height: 40px;
  border: 1px solid #d3cdcd;
  border-radius: 5px;
  padding: 8px 0 0 7px;
}
.c2_button {
  height: 25px;
  margin: 8px 5px 0 35px;
}
.c2_button1 {
  height: 25px;
  margin: 8px 5px 0 0;
}
.content2_1 {
  margin-top: 3px;
}
.date_1 {
  margin: 3px 6px 0 0;
}
.footView span {
  float: left;
  margin-right: 48px;
  line-height: 6vh;
  font-size: 14px;
}
.footView span i {
  margin-right: 5px;
}
</style>

<style>
.wDele .el-input__icon {
  line-height: 30px;
}
.wDele .w-top .sty .el-input__inner {
  height: 30px;
  width: 100px;
}
.wDele .w-top .ph .el-input__inner {
  height: 30px;
  width: 160px;
}
.wDele .w-top .ph .el-date-editor.el-input,
.wDele .w-top .ph .el-date-editor.el-input__inner {
  width: 160px;
}
.wDele .c_b .el-button {
  padding: 0;
}
.wDele .content1_1 .el-input {
  width: 223px;
  margin-left: 10px;
}
.wDele .content1_1 .el-input__inner {
  height: 30px;
}
.wDele .c2_2 .check {
  margin: 0 16px 0 0;
  width: 70px;
  height: 30px;
  padding: 3px 0 0 7px;
}

.wDele .c2_2 .el-button {
  width: 40px;
}
.wDele .c2 .el-button {
  margin: 0 10px 0 0;
}
</style>

<template>
  <div class="wDele">
    <div class="w-top">
      <p class="t_content">货号</p>
      <input class="inp inp2" type="text" v-model="kindsNumber" />
    </div>

    <div class="deleteDocument_c">
      <div class="content1">
        <div class="content1_1">
          <el-button @click="reduce"
            ><i class="el-icon-remove-outline"></i> 减标</el-button
          >
        </div>
        <div class="c_tab">
          <el-table
            :data="rfidList"
            style="width: 100%"
            highlight-current-row
            height="630"
            @row-click="rowClick"
            show-summary
            :header-cell-style="{
              background: '#f4f4f4',
              padding: 0,
              height: '40px'
            }"
            :cell-style="{ padding: 0, height: '26px' }"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="50"></el-table-column>
            <el-table-column
              show-overflow-tooltip
              type="index"
              label="序号"
              width="50"
            ></el-table-column>
            <el-table-column
              show-overflow-tooltip
              prop="showCode"
              label="RFID"
            ></el-table-column>
            <el-table-column
              show-overflow-tooltip
              prop="kindsNumber"
              label="货号"
            ></el-table-column>
            <el-table-column
              show-overflow-tooltip
              prop="number"
              label="数量"
              width="50"
            ></el-table-column>
            <el-table-column
              show-overflow-tooltip
              prop="sellingPrice"
              label="售价"
              width="100"
            ></el-table-column>
          </el-table>
        </div>
      </div>

      <div class="content2">
        <div class="c2">
          <div class="c2_1">
            <el-button @click="search" type="primary" icon="el-icon-search"
              >查询</el-button
            >
            <el-button @click="dele" icon="el-icon-delete">删除</el-button>
            <el-button @click="clean" icon="el-icon-delete">清空</el-button>
          </div>

          <div class="c2_2">
            <p>单据序号：{{ sequence }}</p>
          </div>
        </div>

        <div class="content2_1">
          <el-table
            id="outTable"
            :data="tableData2"
            style="width: 100%"
            height="630"
            highlight-current-row
            :header-cell-style="{
              background: '#f4f4f4',
              padding: 0,
              height: '40px'
            }"
            :cell-style="{ padding: 0, height: '26px' }"
            @selection-change="handleSelectionChangeRight"
          >
            <el-table-column type="selection" width="60"></el-table-column>
            <el-table-column
              type="index"
              label="序号"
              width="50"
            ></el-table-column>
            <template v-for="i in tableHead">
              <el-table-column
                :prop="i.prop"
                :label="i.label"
                :width="i.width"
                show-overflow-tooltip
                :key="i.prop + Math.random()"
              >
              </el-table-column>
            </template>
          </el-table>
        </div>
      </div>
    </div>

    <!-- <foot-view class="footView">
      <div slot="text">
        <span>数据库：{{jxQuarter}}</span>
      </div>
    </foot-view> -->

    <!-- 分頁 -->
    <foot-view
      class="footView"
      :pagesNum="pagesNum"
      :hasPagination="true"
      :pageSize="limit"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    >
      <div slot="text">
        <el-popover
          placement="top-start"
          width="120"
          max-height="100px"
          trigger="click"
        >
          <i slot="reference" class="el-icon-date"> 表头配置</i>
        </el-popover>
        <span>数据库：{{ jxQuarter }}</span>
      </div>
    </foot-view>
  </div>
</template>

<script>
import footView from '../../components/FootView'
import {
  InventoryQuary,
  getDetailed,
  DeleteDishdifference,
  getWarehouseTab,
  DeleteTackStock,
  DeleteTransfers,
  getXHRfid,
  reduaceRfid,
  getPdRfid,
  Reduace,
  clearBoth,
  Empty
} from '../../network/manufacturer'
import { getRightData, deleteShop } from '../../network/shop'
export default {
  name: 'wDele',
  created() {
    let jxQuarterVal = document.cookie.match(RegExp('jxQuarter' + '=([^;]+)'))
    this.jxQuarter = jxQuarterVal ? jxQuarterVal[1] : null
    
    this.sequence = this.$route.query.sequenceid
    this.type = this.$route.query.type
    this.warehouseNo = this.$route.query.warehouseNo
    console.log(this.sequence, this.type)
    // this.$router.go(0)
    this.getHead()
    //仓库
    // getWarehouse().then(res => {
    //     res.data.data.forEach(e => {
    //       this.warehouseList.push({value:e.no,label:e.name})//调出
    //     })
    // })
  },
  data() {
    return {
      allData: [],
      //数据总条数
      dataLen: 0,
      //总页码数
      pagesNum: 0,
      //每页显示条数
      limit: 200,
      // 当前页码
      currentPage: 1,

      warehouseList: [],
      kindsNumber: '',
      type: '',
      sequence: '',
      tableHead: [],
      tableData2: [],
      allData: [],
      selectedList: [],
      // jxQuarter: document.cookie.match(RegExp('jxQuarter' + '=([^;]+)'))[1],
      jxQuarter: null,
      rfidList: [],
      rfidSelect: [],
      multipleSelection: [],
      selectAll: false, //是否全选
      selectAll1: false
    }
  },
  components: {
    footView
  },
  watch: {
    dataLen(a, b) {
      this.pagesNum = Math.ceil(this.dataLen / this.limit)
    },
    limit(a, b) {
      this.pagesNum = Math.ceil(this.dataLen / this.limit)
    }
  },
  methods: {
    handleSizeChange(val) {
      //每页多少条
      this.limit = val
      //总页码
      this.pagesNum = Math.ceil(this.dataLen / this.limit)
      switch (Number(this.type)) {
        case 1:
          this._InventoryQuary()
          // this._getPdRfid()
          break
        case 2:
          this._getDetailed()
          break
        case 3:
          this._getWarehouseTab()
          // this._getXHRfid()
          break
        case 4:
          this._getRightData()
          break
      }
    },
    handleCurrentChange(val) {
      //当前页
      this.currentPage = val
      switch (Number(this.type)) {
        case 1:
          this._InventoryQuary()
          // this._getPdRfid()
          break
        case 2:
          this._getDetailed()
          break
        case 3:
          this._getWarehouseTab()
          // this._getXHRfid()
          break
        case 4:
          this._getRightData()
          break
      }
    },

    rowClick(e) {
      console.log(e)
      // this.kindsNumber =
    },
    //盘点查询
    _InventoryQuary() {
      InventoryQuary({
        sequence: this.sequence,
        'pageData.page': 1,
        'pageData.limit': 20,
        warehouseNo: this.warehouseNo
      }).then(res => {
        if (res.data.data[0].warehouseCheckList != '') {
          this.tableData2 = res.data.data[0].warehouseCheckList
          this.dataLen = res.data.data.length
        }
        if (res.data.data[0].length == 0) {
          this.tableData2 = []
          this.dataLen = 0
        }
      })
    },
    //盘点 Rfid
    _getPdRfid() {
      getPdRfid({ sequence: this.sequence }).then(res => {
        console.log(res.data)
        if (res.data.length != 0) {
          this.rfidList = res.data.data
        }
      })
    },
    //盘差查询
    _getDetailed() {
      let pageData = {
        limit: this.limit,
        page: this.currentPage
      }
      getDetailed({ sequence: this.sequence, pageData }).then(res => {
        console.log(res.data)
        // 不能直接遍历，全部清除返回的数组的值是一个null,不是一个空数组，所以要判断再遍历 （如果返回的是这个，直接清空，并且return）
        if (res.data.data === null || res.data.data === '') {
          this.tableData2 = []
          this.dataLen = 0
          return
        }
        // 不返回null才能走下面逻辑
        for (let a of res.data.data) {
          // 拿到返回的盘差明细，在赋值给表格
          this.tableData2 = a.dataList
          this.dataLen = res.data.data.length
        }
        if (res.data.data.length == 0) {
          this.tableData2 = []
          this.dataLen = 0
        }
      })
    },
    //调拨查询
    _getWarehouseTab() {
      getWarehouseTab({ allotSequence: this.sequence }).then(res => {
        // console.log(res.data)
        this.tableData2 = res.data.data[0].warehouseAllotInfoOneList
        // console.log(this.tableData2)
        if (res.data.data.length == 0) {
          this.tableData2 = []
        }
      })
    },
    _getXHRfid() {
      getXHRfid({ allotSequence: this.sequence }).then(res => {
        // console.log(res.data)
        if (res.data.length != 0) {
          this.rfidList = res.data.data
        }
      })
    },
    //店铺盘点查询
    _getRightData() {
      let pageData = {
        page: 1,
        limit: 99999
      }
      getRightData(
        JSON.stringify({ sequenceList: [this.sequence], type: 0, pageData })
      ).then(res => {
        console.log(res.data)
        this.allData = res.data.data
        this.dataLen = this.allData.length

        console.log(this.dataLen)
        // this.dataLen = this.allData.length
        this.tableData2 = this.allData.slice(
          (this.currentPage - 1) * this.limit,
          this.currentPage * this.limit
        )
      })
    },

    getHead() {
      switch (Number(this.type)) {
        case 1:
          this.tableHead = [
            { prop: 'sequence', label: '仓库盘点序号', width: '130' },
            { prop: 'receiptsNumber', label: '单号', width: '130px' },
            { prop: 'warehouseNo', label: '仓库编号' },
            { prop: 'warehouseName', label: '仓库' },
            { prop: 'kindsNumber', label: '货号', width: '145' },
            { prop: 'stockingNumber', label: '盘点数量' },
            { prop: 'stockingMoney', label: '盘点金额' },
            { prop: 'sellingPrice', label: '售价' },
            { prop: 'quarterName', label: '季度' },
            { prop: 'operator', label: '操作员' },
            { prop: 'enteringTime', label: '录入日期' }
          ]
          this._InventoryQuary()
          this._getPdRfid()
          break
        case 2:
          this.tableHead = [
            { prop: 'sequence', label: '仓库盘点序号', width: '135' },
            { prop: 'receiptsNumber', label: '单号' },
            { prop: 'warehouseNo', label: '仓库编号' },
            { prop: 'warehouseName', label: '仓库' },
            { prop: 'kindsNumber', label: '货号', width: '145' },
            { prop: 'sellingPrice', label: '售价' },
            { prop: 'onePrice', label: '单价' },
            { prop: 'differNumber', label: '盘差数量' },
            { prop: 'differMoney', label: '盘差金额' },
            { prop: 'differTime', label: '盘点日期', width: '100px' },
            { prop: 'curency', label: '币种' },
            { prop: 'operator', label: '操作员' },
            { prop: 'enteringTime', label: '录入时间', width: '160px' },
            { prop: 'status', label: '修改状态' }
          ]
          this._getDetailed()
          break
        case 3:
          this.tableHead = [
            { prop: 'allotSequence', label: '调拨序号', width: '140' },
            { prop: 'receiptsNumber', label: '单号' },
            { prop: 'foldWarehouse', label: '调入仓库' },
            { prop: 'foldWarehouseName', label: '调入仓名' },
            { prop: 'calloutWarehouse', label: '调出仓库' },
            { prop: 'calloutWarehouseName', label: '调出仓名' },
            { prop: 'kindsNumber', label: '货号', width: '145' },
            { prop: 'number', label: '数量', width: '50' },
            { prop: 'allotTime', label: '调拨日期' }
          ]
          this._getWarehouseTab()
          this._getXHRfid()
          break
        case 4:
          this.tableHead = [
            { prop: 'sequence', label: '盘点序号', width: '130' },
            { prop: 'kindsNumber', label: '货号', width: '140' },
            { prop: 'sellingPrice', label: '售价' },
            { prop: 'onePrice', label: '单价' },
            { prop: 'number', label: '盘点数量' },
            { prop: 'stockingMoney', label: '盘点金额' },
            { prop: 'checkTime', label: '盘点日期', width: '100' },
            { prop: 'quarterName', label: '季度' },
            { prop: 'operator', label: '操作员' },
            { prop: 'enteringTime', label: '录入时期', width: '100' }
          ]
          this._getRightData()
          break
      }
    },
    //删除
    dele() {
      console.log(this.type)
      // console.log(this.selectedList, 'selectedList') // 被勾选表格中row的数据,是一个数组,里面item是对象，包含每一行被选中的数据
      // 没有选中数据，直接return
      if (this.selectedList == '') {
        return
      }
      var kindsNumberMap = {}
      // this.type === 2 证明是那边
      switch (Number(this.type)) {
        case 1:
          if (this.selectAll == false) {
            this.selectedList.forEach(e => {
              kindsNumberMap[e.kindsNumber] = e.stockingNumber
            })
            DeleteTackStock(
              JSON.stringify({
                sequence: this.sequence,
                kindsNumberMap: kindsNumberMap
              })
            ).then(res => {
              console.log(res.data)
              if (res.data.code == 200) {
                this.$message({
                  type: 'success',
                  message: '删除成功'
                })
                this._InventoryQuary()
                this._getPdRfid()
                this.kindsNumber = ''
                console.log('顺利清空')
                // 清空原来的数组
                this.tableData2 = []
              }
            })
          }
          //如果全选 清空
          if (this.selectAll == true) {
            Empty({ sequence: this.sequence }).then(res => {
              console.log(res.data)
              if (res.data.code == 200) {
                this.$message({
                  type: 'success',
                  message: '删除成功'
                })
                // this._InventoryQuary()
                // this._getPdRfid()
                this.rfidList = []
                this.tableData2 = []
              }
            })
          }

          break
        case 2:
          console.log('我是case2')
          console.log(this.selectAll, 'selectAll')
          this.selectedList.forEach(e => {
            kindsNumberMap[e.kindsNumber] = e.differNumber
          })
          console.log(kindsNumberMap, 'kindsNumberMap')
          console.log(this.kindsNumber, 'this.kindsNumber')
          DeleteDishdifference(
            JSON.stringify({
              sequence: this.sequence,
              kindsNumberMap: kindsNumberMap
            })
          ).then(res => {
            console.log(res.data)
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功'
              })
              // 删除成功之后,马上又发了一次盘差查询，拿到返回的数据，然后进行填充
              this._getDetailed()
              console.log(this.kindsNumber, 'kindsNumber')
              this.kindsNumber = ''
              // 新增
              // console.log('准备清除数组')
              // this.tableData2 = []
            }
          })
          break
        case 3:
          if (this.selectAll == false) {
            this.selectedList.forEach(e => {
              kindsNumberMap[e.kindsNumber] = e.number
            })
            DeleteTransfers(
              JSON.stringify({
                allotSequence: this.sequence,
                kindsNumberMap: kindsNumberMap
              })
            ).then(res => {
              console.log(res.data)
              if (res.data.code == 200) {
                this.$message({
                  type: 'success',
                  message: '删除成功'
                })
                this._getWarehouseTab()
                this._getXHRfid()
                this.kindsNumber = ''
              }
            })
          }
          if (this.selectAll == true) {
            console.log(this.sequence)
            clearBoth({ allotSequence: this.sequence }).then(res => {
              console.log(res.data)
              if (res.data.code == 200) {
                this.$message({
                  type: 'success',
                  message: '删除成功'
                })
                // this._getWarehouseTab()
                // this._getXHRfid()
                this.rfidList = []
                this.tableData2 = []
              }
            })
          }
          break
        case 4:
          let checkMap = {}
          this.selectedList.forEach(e => {
            checkMap[e.kindsNumber] = e.number
          })
          let vipWarehouseCheck = { sequence: this.sequence }
          let obj = { vipWarehouseCheck, checkMap }
          deleteShop(JSON.stringify(obj)).then(res => {
            console.log(res)
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功'
              })
              this._getRightData()
              this.kindsNumber = ''
            } else {
              this.$message.error({ message: '删除失败', offset: '400' })
            }
          })
          break
      }
    },

    //清空
    clean() {
      let kindsNumberMap = {}

      this.tableData2.forEach(e => {
        kindsNumberMap[e.kindsNumber] = e.differNumber
      })
      switch (this.type) {
        case 1: // 盘点
          Empty({ sequence: this.sequence }).then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '清空成功'
              })
              this.rfidList = []
              this.tableData2 = []
            } else {
              this.$message.error({ message: '清空失败', offset: '400' })
            }
          })
          break
        case 2: // 盘差
          DeleteDishdifference(
            JSON.stringify({
              kindsNumberMap,
              sequence: this.sequence
            })
          ).then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '清空成功'
              })
              this.tableData2 = []
              this.kindsNumber = ''
            } else {
              this.$message.error({ message: '清空失败', offset: '400' })
            }
          })
          break

        default:
          break
      }
    },

    //减标
    reduce() {
      if (this.rfidSelect == '') {
        return
      }
      var rfidList1 = []
      var showCodeList = []
      switch (Number(this.type)) {
        case 1:
          if (this.selectAll1 == false) {
            this.rfidSelect.forEach(e => {
              showCodeList.push(e.showCode)
            })
            var ol1 = { showCodeList, sequence: this.sequence }
            console.log(JSON.stringify(ol1))
            Reduace(JSON.stringify(ol1)).then(res => {
              console.log(res.data)
              if (res.data.code == 200) {
                this.$message({
                  type: 'success',
                  message: '减标成功'
                })
                this._InventoryQuary()
                this._getPdRfid()
              }
            })
          }
          //如果是全选状态 清空
          if (this.selectAll1 == true) {
            Empty({ sequence: this.sequence }).then(res => {
              console.log(res.data)
              if (res.data.code == 200) {
                this.$message({
                  type: 'success',
                  message: '删除成功'
                })
                // this._InventoryQuary()
                // this._getPdRfid()
                this.rfidList = []
                this.tableData2 = []
              }
            })
          }
          break
        case 2:
          break
        case 3:
          //调拨
          if (this.selectAll1 == false) {
            this.rfidSelect.forEach(e => {
              rfidList1.push(e.id)
            })
            console.log(this.sequence, rfidList1)
            reduaceRfid({
              allotSequence: this.sequence,
              rfidList: rfidList1
            }).then(res => {
              console.log(res.data)
              if (res.data.code == 200) {
                this.$message({
                  type: 'success',
                  message: '减标成功'
                })
                this._getWarehouseTab()
                this._getXHRfid()
              }
            })
          }
          //全选状态 清空
          if (this.selectAll1 == true) {
            clearBoth({ allotSequence: this.sequence }).then(res => {
              console.log(res.data)
              if (res.data.code == 200) {
                this.$message({
                  type: 'success',
                  message: '删除成功'
                })
                // this._getWarehouseTab()
                // this._getXHRfid()
                this.rfidList = []
                this.tableData2 = []
              }
            })
          }
          break
      }
    },
    //查询
    search() {
      for (let i of this.tableData2) {
        if (this.kindsNumber == i.kindsNumber) {
          this.tableData2 = [i]
        }
        if (this.kindsNumber == '') {
          this.$router.go(0)
        }
      }
    },
    handleSelectionChange(e) {
      this.rfidSelect = e
      if (this.rfidList.length == this.rfidSelect.length) {
        console.log('全选')
        this.selectAll1 = true
      }
      // console.log(this.rfidSelect)
    },
    //选中
    handleSelectionChangeRight(e) {
      // console.log(e, 'e')
      this.selectedList = e // 被勾选表格中row的数据
      if (this.tableData2.length == this.selectedList.length) {
        console.log('全选')
        this.selectAll = true
      }
      // console.log(this.selectedList)
    }
  },
  watch: {
    type(a, b) {
      this.sequence = this.$route.query.sequenceid
      this.type = this.$route.query.type
      console.log(this.sequence, this.type)
      console.log(a, b)
      this.getHead()
    },
    $route(to, from) {
      console.log(to, from)
      this.$router.go(0)
    }
  },
  mounted() {}
}
</script>
